import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

/**
 * 测试高校录取分数线API调用
 */
public class TestCollegeScoreAPI {
    
    private static final String API_BASE = "https://api.gugudata.com";
    private static final String API_KEY = "E6X2G4QC2VC2LA3WN3K46KLCVAZKEXLM";
    
    public static void main(String[] args) {
        try {
            // 测试按省份查询
            testByProvince();
            
            Thread.sleep(2000); // 等待2秒避免频率限制
            
            // 测试按高校名称查询
            testByCollegeName();
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    /**
     * 测试按省份查询
     */
    private static void testByProvince() {
        try {
            String searchtype = "PROVINCENAME";
            String keyword = "江苏";
            int year = 2024;
            int pageindex = 1;
            int pagesize = 5;
            
            String url = API_BASE + "/metadata/ceecollegeline" +
                    "?appkey=" + API_KEY +
                    "&searchtype=" + URLEncoder.encode(searchtype, "UTF-8") +
                    "&keyword=" + URLEncoder.encode(keyword, "UTF-8") +
                    "&year=" + year +
                    "&pageindex=" + pageindex +
                    "&pagesize=" + pagesize;
            
            System.out.println("测试按省份查询:");
            System.out.println("URL: " + url);
            
            String response = sendGetRequest(url);
            System.out.println("响应: " + response.substring(0, Math.min(500, response.length())) + "...");
            System.out.println();
            
        } catch (Exception e) {
            System.err.println("按省份查询测试失败: " + e.getMessage());
        }
    }
    
    /**
     * 测试按高校名称查询
     */
    private static void testByCollegeName() {
        try {
            String searchtype = "COLLEGENAME";
            String keyword = "北京大学";
            int year = 2024;
            int pageindex = 1;
            int pagesize = 5;
            
            String url = API_BASE + "/metadata/ceecollegeline" +
                    "?appkey=" + API_KEY +
                    "&searchtype=" + URLEncoder.encode(searchtype, "UTF-8") +
                    "&keyword=" + URLEncoder.encode(keyword, "UTF-8") +
                    "&year=" + year +
                    "&pageindex=" + pageindex +
                    "&pagesize=" + pagesize;
            
            System.out.println("测试按高校名称查询:");
            System.out.println("URL: " + url);
            
            String response = sendGetRequest(url);
            System.out.println("响应: " + response.substring(0, Math.min(500, response.length())) + "...");
            System.out.println();
            
        } catch (Exception e) {
            System.err.println("按高校名称查询测试失败: " + e.getMessage());
        }
    }
    
    /**
     * 发送GET请求
     */
    private static String sendGetRequest(String urlString) throws Exception {
        URL url = new URL(urlString);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        
        // 设置请求方法
        connection.setRequestMethod("GET");
        
        // 设置请求头
        connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36");
        connection.setRequestProperty("Accept", "application/json");
        
        // 获取响应
        int responseCode = connection.getResponseCode();
        System.out.println("响应码: " + responseCode);
        
        BufferedReader reader;
        if (responseCode >= 200 && responseCode < 300) {
            reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        } else {
            reader = new BufferedReader(new InputStreamReader(connection.getErrorStream()));
        }
        
        StringBuilder response = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            response.append(line);
        }
        reader.close();
        
        return response.toString();
    }
}
